package org.dant.ant.extension.tasks;

import java.io.*;
import java.util.*;

import org.dant.ant.extension.tasks.commands.AntCallCommand;
import org.dant.ant.extension.tasks.internal.Endpoint;
import org.dant.ant.extension.tasks.internal.RemoteCommand;
import org.dant.ant.extension.types.AntParameter;

public class RemoteAntCall extends RemoteTask {

    protected Map parameters = new HashMap();
    protected Vector params = new Vector();
    private String buildFile;
    private String target;

    public RemoteAntCall() {
        rcmd = new AntCallCommand();
    }

    public void addParam(AntParameter param) {
        this.params.add(param);
    }

    public String getBuildFile() {
        return this.buildFile;
    }
  
    public void setBuildFile(String b) {
        this.buildFile = b;
    }
  
    public String getTarget() {
        return this.target;
    }
  
    public void setTarget(String t) {
        this.target = t;
    }

    public Map getParameters() {
        for (Iterator iter = params.iterator(); iter.hasNext();) {
            AntParameter param = (AntParameter) iter.next();
            parameters.put(param.getName(), param.getValue());
        }
        return this.parameters;
    }

    public void resolveDir() throws IOException {
        for (Iterator iter = destHosts.keySet().iterator(); iter.hasNext();) {
            Endpoint ep = (Endpoint) iter.next();
            RemoteCommand request = (RemoteCommand)destHosts.get(ep);
            File basedir = ep.getBaseDir();
            File buildfile = new File(buildFile);
            if (basedir == null || buildfile.isAbsolute()) {
                continue;
            }
            File absolutePath = fu.resolveFile(basedir, buildFile);
            request.appendParameters("buildfile", absolutePath.getCanonicalPath());
        }
    }
}
